//
//  UIColor+Extension.swift
//  youbao
//
//  Created by Kyle on 16/9/20.
//  Copyright © 2016年 xiaoluuu. All rights reserved.
//

import UIKit

public extension UIColor {
    // Mark: convenience init method
    convenience init(red: Int, green: Int, blue: Int, alpha: Float? = 1.0) {
        assert(red >= 0 && red <= 255, "Invalid red component")
        assert(green >= 0 && green <= 255, "Invalid green component")
        assert(blue >= 0 && blue <= 255, "Invalid blue component")
        assert(alpha! >= 0.0 && alpha! <= 1.0, "Invalid alpha component")
        
        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: CGFloat(alpha!))
    }
    
    convenience init(_ hex: Int, alpha: Float? = 1.0) {
        
        self.init(red: (hex >> 16) & 0xff, green: (hex >> 8) & 0xff, blue: hex & 0xff, alpha: alpha)
    }
    
    /// 字符串初始化颜色
    ///
    /// - Parameter rgbaStr: #FFFFFF
    convenience init(_ rgbaStr: String) {
        let hex = rgbaStr
        
        var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if (cString.hasPrefix("#")) {
            let index = cString.index(cString.startIndex, offsetBy:1)
            cString = cString.substring(from: index)
        }

        if (cString.count != 6) {
            self.init(0x000000)
            return
        }

        let rIndex = cString.index(cString.startIndex, offsetBy: 2)
        let rString = cString.substring(to: rIndex)
        let otherString = cString.substring(from: rIndex)
        let gIndex = otherString.index(otherString.startIndex, offsetBy: 2)
        let gString = otherString.substring(to: gIndex)
        let bIndex = cString.index(cString.endIndex, offsetBy: -2)
        let bString = cString.substring(from: bIndex)

        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)

        self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
    }
}

extension UIScreen {
    static var bnty_width: CGFloat {
        get {
            return UIScreen.main.bounds.width
        }
    }
    static var bnyt_height: CGFloat {
        get {
            return UIScreen.main.bounds.height
        }
    }
    
    static var bnyt_navigationBar: CGFloat {
        get {
            return self.bnyt_fullScreen ? 88 : 64
        }
    }
    static var bnyt_tabBar: CGFloat {  //底部tabbar
        get {
            return self.bnyt_fullScreen ? 83 : 49
        }
    }
    static var bnyt_indactorBar: CGFloat {  //底部指示条
        get {
            return self.bnyt_fullScreen ? 34 : 0
        }
    }
    static var bnyt_statusBar: CGFloat {  //状态栏
        get {
            return self.bnyt_fullScreen ? 44 : 20
        }
    }
    
    static var bnyt_fullScreen: Bool {  //异型屏(刘海屏)
        get {
            if #available(iOS 11, *) {
                guard let w = UIApplication.shared.delegate?.window, let unwrapedWindow = w else {
                    return false
                }
                
                if unwrapedWindow.safeAreaInsets.left > 0 || unwrapedWindow.safeAreaInsets.bottom > 0 {
                    print(unwrapedWindow.safeAreaInsets)
                    return true
                }
            }
            return false
        }
    }
}

//MARK: - UIImage
extension UIImage {
    class func createImage(_ color: UIColor)-> UIImage{
        let rect = CGRect.init(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}


